/**
 * Copyright phonemap.com
 *
 * Licensed under the phonemap.com License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.phonemap.com/licenses/LICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonemap;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.GeoPt;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.search.GeoPoint;
import com.phonemap.common.Util;
import com.phonemap.models.User;

/**
 * The Class SearchServlet.
 */
@SuppressWarnings("serial")
public class SearchServlet extends BaseServlet {
	
	/** The Constant logger. */
	private static Logger logger = Logger.getLogger(FollowServlet.class
			.getCanonicalName());
	
	/**
	 * Get the requested customer entities in JSON format.
	 *
	 * @param req the req
	 * @param resp the resp
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	protected final void doGet(final HttpServletRequest req, final HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp);
		logger.log(Level.INFO, "Obtaining Follow listing");
//		Follow.deleteAllFollows();
		
		String action = "";
		if (req.getParameter("action") != null) {
			action = req.getParameter("action");
		}
		PrintWriter out = resp.getWriter();
		String userId = req.getParameter("userId");
		List<Entity> entities = new ArrayList<Entity>();
		if (action.equalsIgnoreCase("getUsers")) {
			String name = req.getParameter("textSearch");
			int offset = Integer.parseInt(req.getParameter("offset"));
			if (name != null && !name.equalsIgnoreCase("")) {
				entities = User.getUsers(name, FilterOperator.GREATER_THAN, offset);
				//entities = User.getUsers(fullname, operator, offset);
			} 
		} else if (action.equalsIgnoreCase("initMap")) {
			Entity user = User.getUser(Long.parseLong(userId));
			if (user != null) {
				entities.add(user);
			} 
		} else if (action.equalsIgnoreCase("getUsersByIndex")) {
			String name = req.getParameter("textSearch");
			int offset = Integer.parseInt(req.getParameter("offset"));
			if (name != null && !name.equalsIgnoreCase("")) {
				entities = User.getUsers(userId, name, offset);
			} 
		} else if (action.equalsIgnoreCase("getUsersByKm")) {
//			Entity user = User.getUser(Long.parseLong(userId));
			if (userId != null) {
				double lat = Double.parseDouble(req.getParameter("latitude"));
				double lng = Double.parseDouble(req.getParameter("longitude"));
				int distance = Integer.parseInt(req.getParameter("distance"));
				int offset = Integer.parseInt(req.getParameter("offset"));
				entities = User.getUsers(new GeoPoint(lat, lng), distance, offset);
			} 
		} 
		out.println(Util.writeJSON(entities));
		return;
	}

}
